package com.example.diary;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;

import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import static com.example.diary.DatabaseHelper.DB_NAME;
import static com.example.diary.DatabaseHelper.TABLE_NAME;
import static com.example.diary.DatabaseHelper.VERSION;

/**
 * @author zengzl 日记列表页面
 * 2021-11-22 V1.0版本完成
 */


public class MainActivity extends AppCompatActivity {
    public static DatabaseHelper dbHelper;
    private SQLiteDatabase db;
    private List<String> diary = new ArrayList<>();
    public static final int TAG_INSERT = 1;
    public static final int TAG_UPDATE = 0;
    public static final int TAG_GOBACK = 2;
    private String select_item;
    private int Id;
    ListView listView;
    ArrayAdapter<String> adapter;

    private SwipeRefreshLayout swipeRefresh;

    public static DatabaseHelper getDbHelper() {
        return dbHelper;
    }
    //about按钮声明
    Button setting;

    public String id_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏状态栏 调试通过，写入ppt
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


        setContentView(R.layout.activity_main);
        //        icon图标引用
        Typeface iconfont = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
        TextView mian_add = (TextView) findViewById(R.id.add);
        mian_add.setTypeface(iconfont);
        TextView setting = (TextView) findViewById(R.id.setting);
        setting.setTypeface(iconfont);



        Button add = findViewById(R.id.add);

        Intent intent = getIntent();
        id_name = intent.getStringExtra("id_name");
//        Toast.makeText(MainActivity.this, "日记主页面，登录成功，登录名：" + id_name, Toast.LENGTH_SHORT).show();

        swipeRefresh = findViewById(R.id.swipe_refresh);
        // 设置刷新监听器，用户可以手动下拉刷新
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout
                .OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        dbHelper = new DatabaseHelper(MainActivity.this, DB_NAME, null, VERSION);
        dbHelper.getWritableDatabase();
        init();
        //添加笔记
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Detail.class);
                intent.putExtra("id_name",id_name);
                intent.putExtra("TAG", TAG_INSERT);
                startActivity(intent);
            }
        });
        //设置列表项目点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent(MainActivity.this, Detail.class);
                Id = getDiaryId(position);
                //  Log.d("MainActivity",""+id);
                intent.putExtra("ID", Id);
                intent.putExtra("TAG", TAG_UPDATE);
                intent.putExtra("id_name", id_name);
                startActivity(intent);
                // 跳转至写日记界面，改界面活动结束
                finish();
            }
        });
        /*about跳转*/
        setting=findViewById(R.id.setting);
        //监听事件
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent i=new Intent(MainActivity.this,About.class);
            startActivity(i);
            }
        });


    }


    private void init() {
        db = dbHelper.getWritableDatabase();
        diary.clear();
        //查询数据库，将title一列添加到列表项目中
        // id 倒序，新建的文件默认在第一个
        Cursor cursor = db.query(TABLE_NAME, null, "author="+"'"+id_name+"'"+" or author='admin'", null, null, null, "id DESC");
        if (cursor.moveToFirst()) {
            String diary_item;
            do {
                diary_item = cursor.getString(cursor.getColumnIndex("title"));
                diary.add(diary_item);
            } while (cursor.moveToNext());
        }
        cursor.close();
        adapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_list_item_1, diary);
        listView = findViewById(R.id.list_item);
        listView.setAdapter(adapter);
    }

    //刷新列表
    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        init();
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

    private int getDiaryId(int position) {
        //获取所点击的日记的title
        int Id;
        select_item = diary.get(position);
        //获取id
        db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query(TABLE_NAME, new String[]{"id"}, "title=?",
                new String[]{select_item}, null, null, null);
        cursor.moveToFirst();
        Id = cursor.getInt(cursor.getColumnIndex("id"));

        return Id;
    }
    

}

